//
//  LikelyTarbar.m
//  LikelyTarbar
//
//  Created by Ju on 15/3/30.
//  Copyright (c) 2015年 juwenz. All rights reserved.
//

#import "LikelyTarbar.h"

#define LTICONHEIGHT 30.0f
#define LTICONWIDTH 30.0f
#define LTTABBARHEIGHT 50.0f
#define LTIMAGEVIEWTAG 100
#define LTTITLETAG 120
#import "LikelyTabIconModel.h"

NSString *toSafelyString(NSString *str)
{
    return str == nil ? @"" : str;
}

@implementation LikelyTarbar
{
    LTTabIconType selectedType;
    NSMutableDictionary *iconDic;
}
@synthesize icons = _icons;
@synthesize selectedType = selectedType;
- (instancetype)initWithFrame:(CGRect)frame
{
    frame.origin.y = frame.size.height - LTTABBARHEIGHT;
    frame.size.height = LTTABBARHEIGHT;
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor lightGrayColor];
        iconDic = [NSMutableDictionary dictionaryWithCapacity:5];
    }
    return self;
}

- (void)setIcons:(NSArray *)icons
{
    _icons = icons;
    [self setupIcons];
}

- (void)setupIcons
{
    CGRect frame = self.bounds;
    frame.size.width = frame.size.width / _icons.count;
    for (NSInteger i = 0, len = _icons.count; i < len; i++) {
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        LikelyTabIconModel *model = _icons[i];
        frame.origin.x = frame.size.width * i;
        button.frame = frame;
        button.tag = model.type;
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImage *image = [UIImage imageNamed:model.imageName];
        UIImage *hightlightedImage = [UIImage imageNamed:model.highlightedImageName];
        CGRect imageViewFrame = button.bounds;
        imageViewFrame.size.width = LTICONWIDTH;
        imageViewFrame.size.height = LTICONHEIGHT;
        imageViewFrame.origin.y = 3;
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image highlightedImage:hightlightedImage];
        imageView.frame = imageViewFrame;
        imageView.tag = LTIMAGEVIEWTAG;
        imageView.center = CGPointMake(button.frame.size.width / 2.0f, imageView.center.y);
        [button addSubview:imageView];
        
        UILabel *titleLbl = [[UILabel alloc] init];
        titleLbl.font = [UIFont systemFontOfSize:10.0];
        titleLbl.tag = LTTITLETAG;
        titleLbl.textAlignment = NSTextAlignmentCenter;
        titleLbl.textColor = [UIColor whiteColor];
        titleLbl.highlightedTextColor = [UIColor purpleColor];
        
        imageViewFrame.origin.y +=  imageViewFrame.size.height + 3;
        imageViewFrame.size.height = 10;
        imageViewFrame.size.width = button.frame.size.width;
        titleLbl.frame = imageViewFrame;
        titleLbl.text = toSafelyString(model.title);
        [button addSubview:titleLbl];
        
        [self addSubview:button];
        [iconDic setObject:button forKey:@(model.type)];
    }
    
}

- (void)clickButton:(UIButton *)button
{
    if (selectedType == button.tag) {
        return;
    }else{
        [(UIImageView *)[[iconDic objectForKey:@(selectedType)] viewWithTag:LTIMAGEVIEWTAG] setHighlighted:NO];
        [(UIImageView *)[[iconDic objectForKey:@(selectedType)] viewWithTag:LTTITLETAG] setHighlighted:NO];
        [(UIImageView *)[button viewWithTag:LTIMAGEVIEWTAG] setHighlighted:YES];
        [(UIImageView *)[button viewWithTag:LTTITLETAG] setHighlighted:YES];
    }
    selectedType = button.tag;
    
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedAtType:)]) {
        [self.delegate tabBar:self selectedAtType:button.tag];
    }
}

@end
